package io.renren.modules.email;

import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileInputStream;

public class ParseUtil {
    //判断
    public String judge(String path) {
        //先截取
        String substring = path.substring(path.length() - 4, path.length());
        // 再判断是否包含
        if (substring.contains("docx")) {
            File file = new File(path);
            try {
                FileInputStream f = new FileInputStream(file);
                XWPFDocument docu = new XWPFDocument(f);
                XWPFWordExtractor ex =new XWPFWordExtractor(docu);
                //转化为TEXT文件
                String text1 = ex.getText();
                //关闭流
                f.close();
                return text1;

            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{//如果不是
            File file = new File(path);
            try{
                FileInputStream f = new FileInputStream(file);
                WordExtractor ex = new WordExtractor(f);
                String text = ex.getText();
                f.close();
                return text;
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
        return null;
    }

}
